﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NBOW
{
    public partial class UpdateBOW : Form
    {
        public UpdateBOW()
        {
            InitializeComponent();
        }

        #region Members

        Common com = new Common();
        ConnectDB connect = new ConnectDB();
        Calculator cal = new Calculator();
        List<string> listWordofBug = new List<string>(); // list all bug

        #endregion

        #region Event handlers

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            openFilePath.ShowDialog();
            string File = openFilePath.FileName;
            if (string.IsNullOrEmpty(File))
                return;
            txtPath.Text = File;
            if (txtPath.Text != string.Empty)
                viewWordUpdate(txtPath.Text);
        }

        private void btnSelectA_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvWord.Items.Count; i++)
            {
                lvWord.Items[i].Checked = true;
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lvWordBag.Items.Count; i++)
            {
                lvWordBag.Items[i].Checked = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> dic_temp = new Dictionary<string, int>();
            for (int i = 0; i < lvWord.Items.Count; i++)
            {
                if (lvWord.Items[i].Checked == true)
                {
                    dic_temp.Add(lvWord.Items[i].Text, int.Parse(lvWord.Items[i].SubItems[1].Text));
                    lvWord.Items[i].Remove();
                    i--;
                }
            }
            foreach (string item in dic_temp.Keys)
            {
                int vl = int.Parse(dic_temp[item].ToString());
                ListViewItem showitem = new ListViewItem(item);
                ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, vl.ToString());
                showitem.SubItems.Add(sbitem);
                lvWordBag.Items.Add(showitem);
                lvWordBag.CheckBoxes = true;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> temp = new Dictionary<string, int>();
            for (int i = 0; i < lvWord.Items.Count; i++)
            {
                if (lvWord.Items[i].Checked == true)
                {
                    temp.Add(lvWord.Items[i].Text, int.Parse(lvWord.Items[i].SubItems[1].Text));
                    lvWord.Items[i].Remove();
                    i--;
                }
            }
            foreach (string item in temp.Keys)
            {
                int vl = int.Parse(temp[item].ToString());
                ListViewItem showitem = new ListViewItem(item);
                ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, vl.ToString());
                showitem.SubItems.Add(sbitem);
                lvWordBag.Items.Add(showitem);
                lvWordBag.CheckBoxes = true;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lvWordBag.Items.Count > 0)
            {
                Dictionary<string, double> dic_wfile = new Dictionary<string, double>();
                Dictionary<string, double> dic_bag = new Dictionary<string, double>();
                // get word appears how bugs.
                for (int i = 0; i < lvWordBag.Items.Count; i++)
                {
                    string s = lvWordBag.Items[i].Text;
                    int vl = int.Parse(lvWordBag.Items[i].SubItems[1].Text);
                    dic_wfile.Add(s, vl);
                }

                // write frequency file
                string pathfre = Environment.CurrentDirectory + "\\DataTest\\WriteText.txt";
                if (!System.IO.Directory.Exists(pathfre))
                    com.writefile(dic_wfile,pathfre);

                
                DataTable test = connect.getDataFromXLS(txtPath.Text);
                dic_bag = cal.bag(test, dic_wfile, txtPath.Text);

                //write bag of word
                string pathBOW = Environment.CurrentDirectory + "\\DataTest\\BagOfWords.txt";
                if (!System.IO.Directory.Exists(pathBOW))
                    com.writefile(dic_bag, pathBOW);

                lvWordBag.Items.Clear();

                MessageBox.Show("Updated successful");
            }
            else MessageBox.Show("Update fail");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        private void viewWord(string path)
        {
            Dictionary<string, double> dic = new Dictionary<string, double>();
            Dictionary<string, int> show = new Dictionary<string, int>();
            DataTable dtBuild = connect.getDataFromXLS(path);

            //get empty columns
            List<int> getColumns = new List<int>();
            getColumns = com.disField(dtBuild);

            string str_ArrayText = string.Empty;
            if (dtBuild != null)
            {
                for (int i = 0; i < dtBuild.Rows.Count; i++)
                {
                    for (int j = 0; j < dtBuild.Columns.Count; j++)
                    {
                        if (getColumns.Contains(j))
                            continue;
                        str_ArrayText += dtBuild.Rows[i][j].ToString() + " ";
                    }
                }

                listWordofBug = com.processWordofBug(str_ArrayText);

                // get appear frequency of word in bug report
                dic = cal.GetTf_qi(listWordofBug);

                foreach (string item in dic.Keys)
                {
                    show.Add(item, int.Parse(dic[item].ToString()));
                    ListViewItem showitem = new ListViewItem(item.ToString());
                    ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, dic[item].ToString());
                    showitem.SubItems.Add(sbitem);
                    lvWord.Items.Add(showitem);
                }

                lvWord.CheckBoxes = true;
            }
            else
                MessageBox.Show("Empty data");
        }

        private void viewWordUpdate(string path)
        {
            Dictionary<string, double> dicFre = new Dictionary<string, double>();
            Dictionary<string, int> dicShow = new Dictionary<string, int>();
            Dictionary<string, double> dicBOW = new Dictionary<string, double>();

            DataTable dtBuild = connect.getDataFromXLS(path);

            // get empty columns
            List<int> getColumns = new List<int>();
            getColumns = com.disField(dtBuild);

            string str_ArrayText = string.Empty;
            if (dtBuild != null)
            {
                for (int i = 0; i < dtBuild.Rows.Count; i++)
                {
                    for (int j = 0; j < dtBuild.Columns.Count; j++)
                    {
                        if (getColumns.Contains(j))
                            continue;
                        str_ArrayText += dtBuild.Rows[i][j].ToString() + " ";
                    }
                }

                listWordofBug = com.processWordofBug(str_ArrayText);

                // get appear frequency of word in bug report
                dicFre = cal.GetTf_qi(listWordofBug);

                dicBOW = com.readBagOfWord();
                lvWord.Items.Clear();
              foreach (string item in dicFre.Keys)
                {
                    if (int.Parse(dicFre[item].ToString()) > 0)
                    {
                        if (dicBOW.ContainsKey(item))
                        {
                            dicShow.Add(item, int.Parse(dicFre[item].ToString()));
                            ListViewItem showitem = new ListViewItem(item.ToString());
                            ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, dicFre[item].ToString());
                            showitem.SubItems.Add(sbitem);
                            showitem.Checked = true;
                            lvWord.Items.Add(showitem);
                        }
                            //continue;
                        else
                        {
                            dicShow.Add(item, int.Parse(dicFre[item].ToString()));
                            ListViewItem showitem = new ListViewItem(item.ToString());
                            ListViewItem.ListViewSubItem sbitem = new ListViewItem.ListViewSubItem(showitem, dicFre[item].ToString());
                            showitem.SubItems.Add(sbitem);
                            lvWord.Items.Add(showitem);
                        }
                    }
                }

                lvWord.CheckBoxes = true;
            }
            else
            {
                MessageBox.Show("Empty data");
            }
        }

    }
}
